<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家" prop="ulang">
        <el-input
          v-model="queryParams.ulang"
          placeholder="请输入国家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级ID" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入上级ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请码" prop="inviteCode">
        <el-input
          v-model="queryParams.inviteCode"
          placeholder="请输入邀请码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间" prop="addtime">
        <el-input
          v-model="queryParams.addtime"
          placeholder="请输入注册时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户余额" prop="balance">
        <el-input
          v-model="queryParams.balance"
          placeholder="请输入账户余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号冻结金额" prop="freezeBalance">
        <el-input
          v-model="queryParams.freezeBalance"
          placeholder="请输入账号冻结金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当日交易次数" prop="dealCount">
        <el-input
          v-model="queryParams.dealCount"
          placeholder="请输入当日交易次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="dz">
        <el-input
          v-model="queryParams.dz"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jksd:users:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jksd:users:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jksd:users:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jksd:users:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="手机" align="center" prop="tel" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="国家" align="center" prop="ulang" />
      <el-table-column label="上级ID" align="center" prop="parentId" />
      <el-table-column label="邀请码" align="center" prop="inviteCode" />
      <el-table-column label="注册时间" align="center" prop="addtime" />
      <el-table-column label="状态，1启用，2禁用" align="center" prop="status" />
      <el-table-column label="账户余额" align="center" prop="balance" />
      <el-table-column label="账号冻结金额" align="center" prop="freezeBalance" />
      <el-table-column label="是否登录状态，1：是，0否" align="center" prop="loginStatus" />
      <el-table-column label="当日交易次数" align="center" prop="dealCount" />
      <el-table-column label="最后交易日期(年月日)" align="center" prop="dealTime" />
      <el-table-column label="" align="center" prop="ip" />
      <el-table-column label="" align="center" prop="dz" />
      <el-table-column label="" align="center" prop="dzc" />
      <el-table-column label="佣金" align="center" prop="dzy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jksd:users:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jksd:users:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员-用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="国家" prop="ulang">
          <el-input v-model="form.ulang" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="上级ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入上级ID" />
        </el-form-item>
        <el-form-item label="邀请码" prop="inviteCode">
          <el-input v-model="form.inviteCode" placeholder="请输入邀请码" />
        </el-form-item>
        <el-form-item label="注册时间" prop="addtime">
          <el-input v-model="form.addtime" placeholder="请输入注册时间" />
        </el-form-item>
        <el-form-item label="提现密码" prop="pwd2">
          <el-input v-model="form.pwd2" placeholder="请输入提现密码" />
        </el-form-item>
        <el-form-item label="账户余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入账户余额" />
        </el-form-item>
        <el-form-item label="账号冻结金额" prop="freezeBalance">
          <el-input v-model="form.freezeBalance" placeholder="请输入账号冻结金额" />
        </el-form-item>
        <el-form-item label="当日交易次数" prop="dealCount">
          <el-input v-model="form.dealCount" placeholder="请输入当日交易次数" />
        </el-form-item>
        <el-form-item label="" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="dz">
          <el-input v-model="form.dz" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="dzc">
          <el-input v-model="form.dzc" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="佣金" prop="dzy">
          <el-input v-model="form.dzy" placeholder="请输入佣金" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUsers, getUsers, delUsers, addUsers, updateUsers } from "@/api/jksd/users";

export default {
  name: "Users",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员-用户表格数据
      usersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tel: null,
        username: null,
        ulang: null,
        parentId: null,
        inviteCode: null,
        addtime: null,
        status: null,
        balance: null,
        freezeBalance: null,
        dealCount: null,
        ip: null,
        dz: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tel: [
          { required: true, message: "手机不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "上级ID不能为空", trigger: "blur" }
        ],
        inviteCode: [
          { required: true, message: "邀请码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态，1启用，2禁用不能为空", trigger: "change" }
        ],
        pwd2: [
          { required: true, message: "提现密码不能为空", trigger: "blur" }
        ],
        salt2: [
          { required: true, message: "提现密码盐不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员-用户列表 */
    getList() {
      this.loading = true;
      listUsers(this.queryParams).then(response => {
        this.usersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tel: null,
        username: null,
        nickname: null,
        ulang: null,
        pwd: null,
        salt: null,
        parentId: null,
        signiture: null,
        pwdErrorNum: null,
        allowLoginTime: null,
        realName: null,
        idCardNum: null,
        topPic: null,
        botPic: null,
        idStatus: null,
        inviteCode: null,
        addtime: null,
        status: null,
        realTime: null,
        pwd2: null,
        salt2: null,
        headpic: null,
        balance: null,
        freezeBalance: null,
        loginStatus: null,
        rechargeNum: null,
        depositNum: null,
        dealStatus: null,
        dealError: null,
        dealRewardCount: null,
        dealCount: null,
        dealTime: null,
        active: null,
        childs: null,
        kouchuBalance: null,
        kouchuBalanceUid: null,
        showTd: null,
        showCz: null,
        showTx: null,
        showTel: null,
        showNum: null,
        showTel2: null,
        wxEwm: null,
        zfbEwm: null,
        level: null,
        lixibaoBalance: null,
        lixibaoDjBalance: null,
        ip: null,
        isJia: null,
        jb: null,
        pStr: null,
        isDl: null,
        dz: null,
        dzc: null,
        dzy: null,
        zuidi: null,
        num: null,
        zxTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员-用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUsers(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员-用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUsers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUsers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员-用户编号为"' + ids + '"的数据项？').then(function() {
        return delUsers(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jksd/users/export', {
        ...this.queryParams
      }, `users_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
